package cn.seaboot.html;

import cn.seaboot.html.table.*;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * sax 解析 html 文件，并将内容转换成对象
 *
 * @author Mr.css
 * @version 2025-07-01 18:40
 */
public class SaxHtml {

    /**
     * 处理表格
     *
     * @param root 根元素
     * @return 表格
     */
    public static Table parseTable(Element root) {
        List<TItem> items = new ArrayList<>();
        for (Element ele : root.elements()) {
            String name = ele.getName();
            if ("tr".equals(name)) {
                items.add(parseRow(ele));
            } else if ("thead".equals(name)) {
                items.add(parseHead(ele));
            } else if ("tbody".equals(name)) {
                items.add(parseBody(ele));
            } else if ("tfoot".equals(name)) {
                items.add(parseFoot(ele));
            } else if ("caption".equals(name)) {
                items.add(parseCaption(ele));
            } else if ("colspan".equals(name)) {
                items.add(parseColGroup(ele));
            } else {
                throw new IllegalArgumentException("illegal element: " + name);
            }
        }
        return new Table(items);
    }

    /**
     * 标题栏
     *
     * @param ele -
     * @return 标题
     */
    public static Caption parseCaption(Element ele) {
        return new Caption(ele.getText());
    }


    /**
     * 列配置
     *
     * @param ele -
     * @return 列
     */
    public static Col parseCol(Element ele) {
        return new Col();
    }

    /**
     * 列分组
     *
     * @param ele -
     * @return 列分组
     */
    public static ColGroup parseColGroup(Element ele) {
        List<Element> cells = ele.elements();
        List<Col> cols = new ArrayList<>();
        for (Element cell : cells) {
            cols.add(parseCol(cell));
        }
        return new ColGroup(cols);
    }

    /**
     * 处理单元格
     *
     * @param ele -
     * @return -
     */
    public static TD parseCell(Element ele) {
        TD data = new TD();
        data.setValue(ele.getText());

        Attribute attr = ele.attribute("colspan");
        if (attr != null) {
            data.setColspan(Integer.parseInt(attr.getValue()));
        }

        attr = ele.attribute("rowspan");
        if (attr != null) {
            data.setRowspan(Integer.parseInt(attr.getValue()));
        }

        attr = ele.attribute("class");
        if (attr != null) {
            data.setClassName(attr.getValue());
        }

        return data;
    }

    /**
     * 处理一行数据
     *
     * @param ele -
     * @return -
     */
    public static TR parseRow(Element ele) {
        List<Element> cells = ele.elements();
        List<TD> columns = new ArrayList<>();
        for (Element cell : cells) {
            columns.add(parseCell(cell));
        }
        return new TR(columns);
    }

    /**
     * 处理表头
     *
     * @param ele -
     * @return -
     */
    public static THead parseHead(Element ele) {
        List<TR> rows = new ArrayList<>();
        for (Element tr : ele.elements()) {
            rows.add(parseRow(tr));
        }
        return new THead(rows);
    }


    /**
     * 处理表内容
     *
     * @param ele -
     * @return -
     */
    public static TBody parseBody(Element ele) {
        List<TR> rows = new ArrayList<>();
        for (Element tr : ele.elements()) {
            rows.add(parseRow(tr));
        }
        return new TBody(rows);
    }

    /**
     * 处理表尾
     *
     * @param ele -
     * @return -
     */
    public static TFoot parseFoot(Element ele) {
        List<TR> rows = new ArrayList<>();
        for (Element tr : ele.elements()) {
            rows.add(parseRow(tr));
        }
        return new TFoot(rows);
    }
}
